<template>
  <view class="container">
    <!-- 社区公告 -->
    <view class="notice-section">
      <view class="section-header">
        <text class="section-title">社区公告</text>
        <!-- <text class="more-link" @tap="navigateToNoticeList">更多></text> -->
      </view>
      <swiper class="notice-swiper" 
              vertical
              :autoplay="true"
              :interval="3000"
              :duration="500"
              circular
              :display-multiple-items="3">
        <swiper-item v-for="(notice, index) in notices" :key="index">
          <view class="notice-item" @tap="showNoticeDetail(notice)">
            <view class="notice-tag" :class="getNoticeTagClass(notice.title)">
              {{ getNoticeTag(notice.title) }}
            </view>
            <view class="notice-content">
              <text class="notice-title">{{ getNoticeTitleWithoutTag(notice.title) }}</text>
              <text class="notice-date">{{ notice.date }}</text>
            </view>
            <text class="arrow">></text>
          </view>
        </swiper-item>
      </swiper>
    </view>

    <!-- 快捷功能区 -->
    <view class="quick-access">
      <view class="grid-item" @tap="handleFamilyAccess">
        <image src="/static/icons/family.png"></image>
        <text>家庭成员</text>
      </view>
      <view class="grid-item" @tap="handleVisitorAccess">
        <image src="/static/icons/visitor.png"></image>
        <text>访客通行</text>
      </view>
    </view>

    <!-- 功能服务区 -->
    <view class="service-section">
      <view class="section-header">
        <text class="section-title">便捷服务</text>
      </view>
      <view class="service-grid">
        <view class="service-item" v-for="(item, index) in services" 
              :key="index" @tap="navigateToService(item)">
          <image :src="item.icon"></image>
          <text>{{ item.name }}</text>
        </view>
      </view>
    </view>
    
    <!-- 社区服务信息 -->
    <view class="community-info-section">
      <view class="section-header">
        <text class="section-title">社区服务信息</text>
        <!-- <text class="more-link" @tap="navigateToCommunityInfo">更多 ></text> -->
      </view>
      <view class="info-cards">
        <view class="info-card" 
              v-for="(info, index) in communityInfo" 
              :key="index"
              @tap="showCommunityInfoDetail(info)">
          <image :src="info.image" mode="aspectFill" class="card-image"></image>
          <view class="card-content">
            <text class="card-title">{{ info.title }}</text>
            <text class="card-desc">{{ info.description }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      notices: [
        {
          id: 1,
          title: '【活动通知】本周六举办社区文化节，欢迎居民参与',
          content: '尊敬的业主：本周六（3月15日）上午9:00-12:00将在小区中心广场举办"和谐社区文化节"活动，包含文艺表演、手工制作、美食分享等环节，欢迎各位业主携家人参与。',
          date: '2025-03-12'
        },
        {
          id: 2,
          title: '【维修计划】下周二小区供水管道维修通知',
          content: '尊敬的业主：因小区主供水管道需进行例行维护，定于下周二（3月18日）上午9:00-下午15:00暂停供水。请各位业主提前做好储水准备，由此带来的不便，敬请谅解。',
          date: '2025-03-11'
        },
        {
          id: 3,
          title: '【安全提醒】小区电动车充电安全规范',
          content: '尊敬的业主：为防范电动车充电安全隐患，请勿在楼道、楼梯间等公共区域为电动车充电，请使用小区指定的充电桩。违规充电不仅存在火灾风险，还将被物业处以警告或罚款。安全无小事，感谢您的配合。',
          date: '2025-03-10'
        },
        {
          id: 4,
          title: '【物业通知】3月物业费缴纳优惠活动开始',
          content: '尊敬的业主：即日起至3月31日，通过小程序缴纳物业费可享受95折优惠，年付享受额外5%折扣。欢迎各位业主积极参与，如有疑问请联系物业客服。',
          date: '2025-03-10'
        },
        {
          id: 5,
          title: '【环境整治】小区绿化带升级改造工程启动',
          content: '尊敬的业主：为提升小区环境品质，小区绿化带升级改造工程将于本月20日启动，预计工期15天。施工期间可能产生一定噪音，请各位业主给予理解和支持。',
          date: '2025-03-10'
        }
      ],
      services: [
        { name: '生活缴费', icon: '/static/icons/payment2.png', path: '/pages/property/payment' },
        { name: '房屋费', icon: '/static/icons/property-fee.png', path: '/pages/property/management-fee' },
        { name: '停车费', icon: '/static/icons/parking.png', path: '/pages/property/parking-fee' },
        { name: '一键开门', icon: '/static/icons/door.png', path: '/pages/access/index', desc: '未连接设备' },
        { name: '报事报修', icon: '/static/icons/repair.png', path: '/pages/property/repair' },
        { name: '联系物业', icon: '/static/icons/contact.png', path: '/pages/property/contract' }
      ],
      communityInfo: [
        {
          id: 1,
          title: '垃圾分类指南',
          description: '了解如何正确分类垃圾，共建绿色社区',
          image: '/static/image/garbage-sorting.png',
          content: '垃圾分类是指按一定规定或标准将垃圾分类储存、分类投放和分类搬运，从而转变成公共资源的一系列活动的总称。分为可回收垃圾、厨余垃圾、有害垃圾和其他垃圾四类。'
        },
        {
          id: 2,
          title: '紧急联系方式',
          description: '物业、消防、医疗等紧急联系方式',
          image: '/static/image/emergency-contact.jpg',
          content: '物业24小时热线：400-123-4567\n安保部：0755-12345678\n消防报警：119\n医疗急救：120\n匪警：110\n附近医院：市第一人民医院 0755-87654321'
        },
        {
          id: 3,
          title: '社区便民服务',
          description: '快递、洗衣、家政等服务信息',
          image: '/static/image/convenience.png',
          content: '快递驿站：小区南门，营业时间 8:00-20:00\n洗衣服务：小区商业街A12铺，联系电话 13812345678\n家政服务：可通过物业预约，或拨打 13987654321'
        }
      ]
    }
  },
  methods: {
    async getNotices() {
      try {
        // 实际项目中这里会从API获取数据
        // const res = await this.$api.getNotices()
        // this.notices = res.data
        
        // 这里使用的是本地数据，实际开发时可替换为API调用
        console.log('获取公告成功')
      } catch (error) {
        uni.showToast({
          title: '获取公告失败',
          icon: 'none'
        })
      }
    },
    showNoticeDetail(notice) {
      uni.navigateTo({
        url: `/pages/notice/detail?id=${notice.id}`
      })
    },
    navigateToService(service) {
      if(service.desc){
        uni.showToast({
          title: service.desc,
          icon: 'none'
        })
        return
      }
      uni.navigateTo({
        url: service.path
      })
    },
    handleFamilyAccess() {
      uni.navigateTo({
        url: '/pages/property/family'
      })
    },
    handleVisitorAccess() {
      uni.navigateTo({
        url: '/pages/property/visitor'
      })
    },
    navigateToNoticeList() {
      uni.navigateTo({
        url: '/pages/notice/list'
      })
    },
    navigateToCommunityInfo() {
      uni.navigateTo({
        url: '/pages/community/info-list'
      })
    },
    showCommunityInfoDetail(info) {
      uni.navigateTo({
        url: `/pages/community/info-detail?id=${info.id}`
      })
    },
    getNoticeTag(title) {
      // 提取【】中的内容作为标签
      const match = title.match(/【(.+?)】/)
      return match ? match[1] : '通知'
    },
    getNoticeTitleWithoutTag(title) {
      // 移除【】标签部分
      return title.replace(/【.+?】/, '')
    },
    getNoticeTagClass(title) {
      const tag = this.getNoticeTag(title)
      switch(tag) {
        case '活动通知': return 'tag-activity'
        case '维修计划': return 'tag-repair'
        case '安全提醒': return 'tag-safety'
        case '物业通知': return 'tag-property'
        case '环境整治': return 'tag-environment'
        default: return 'tag-default'
      }
    }
  },
  onLoad() {
    this.getNotices()
  }
}
</script>

<style lang="scss">
.container {
  padding: 20rpx;
  background-color: #f8f9fc;
  min-height: 100vh;
}

.header {
  position: relative;
  height: 280rpx;
  overflow: hidden;
  background: linear-gradient(135deg, #6a11cb, #2575fc);
  border-radius: 16rpx;
  padding: 40rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  
  &::after {
    content: '';
    position: absolute;
    right: -100rpx;
    bottom: -100rpx;
    width: 400rpx;
    height: 400rpx;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    z-index: 1;
  }
  
  .header-content {
    position: relative;
    z-index: 2;
    
    .title {
      font-size: 44rpx;
      font-weight: bold;
      color: #fff;
      margin-bottom: 16rpx;
      text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
    }
    
    .subtitle {
      font-size: 26rpx;
      color: rgba(255, 255, 255, 0.9);
      max-width: 70%;
    }
  }
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    position: relative;
    padding-left: 20rpx;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 6rpx;
      height: 30rpx;
      background: #6a11cb;
      border-radius: 3rpx;
    }
  }
  
  .more-link {
    font-size: 26rpx;
    color: #6a11cb;
    display: flex;
    align-items: center;
  }
}

.notice-section {
  background: #fff;
  border-radius: 16rpx;
  padding: 0 20rpx 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  
  .notice-swiper {
    height: 240rpx;
  }
  
  .notice-item {
    display: flex;
    align-items: center;
    // padding: 16rpx 0;
    
    .notice-tag {
      padding: 4rpx 12rpx;
      border-radius: 30rpx;
      font-size: 22rpx;
      color: #fff;
      margin-right: 15rpx;
      flex-shrink: 0;
      
      &.tag-activity {
        background: linear-gradient(to right, #ff9500, #ff5e3a);
      }
      
      &.tag-repair {
        background: linear-gradient(to right, #34c759, #32d74b);
      }
      
      &.tag-safety {
        background: linear-gradient(to right, #ff3b30, #ff2d55);
      }
      
      &.tag-property {
        background: linear-gradient(to right, #007aff, #5ac8fa);
      }
      
      &.tag-environment {
        background: linear-gradient(to right, #5856d6, #af52de);
      }
      
      &.tag-default {
        background: linear-gradient(to right, #8e8e93, #aeaeb2);
      }
    }
    
    .notice-content {
      flex: 1;
      
      .notice-title {
        font-size: 28rpx;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
        color: #333;
      }
      
      .notice-date {
        font-size: 22rpx;
        color: #999;
        margin-top: 6rpx;
      }
    }
    
    .arrow {
      color: #ccc;
      font-size: 24rpx;
      margin-left: 10rpx;
    }
  }
}

.quick-access {
  display: flex;
  justify-content: space-between;
  padding: 30rpx;
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  
  .grid-item {
    display: flex;
    align-items: center;
    width: 40%;
    padding: 24rpx;
    border-radius: 12rpx;
    background: #f8f9fc;
    border: 1rpx solid #eef1f8;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.03);
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
    
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: linear-gradient(135deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
      pointer-events: none;
    }
    
    &:active {
      background: #eef1f8;
      transform: translateY(2rpx);
      box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.02);
    }
    
    image {
      width: 60rpx;
      height: 60rpx;
      margin-right: 20rpx;
      border-radius: 12rpx;
      padding: 12rpx;
      background: linear-gradient(135deg, #6a11cb, #2575fc);
      box-shadow: 0 4rpx 10rpx rgba(106, 17, 203, 0.2);
    }
    
    text {
      font-size: 28rpx;
      font-weight: 500;
      color: #333;
      letter-spacing: 1rpx;
    }
  }
}

.service-section {
  background: #fff;
  border-radius: 16rpx;
  padding: 0 20rpx 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  
  .service-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 30rpx 20rpx;
    
    .service-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 15rpx 10rpx;
      border-radius: 8rpx;
      transition: all 0.3s ease;
      
      &:active {
        background-color: #f5f5f5;
        transform: scale(0.95);
      }
      
      image {
        width: 60rpx;
        height: 60rpx;
        margin-bottom: 12rpx;
        border-radius: 16rpx;
        padding: 20rpx;
        background-color: rgba(106, 17, 203, 0.1);
        box-shadow: 0 4rpx 10rpx rgba(106, 17, 203, 0.15);
      }
      
      text {
        font-size: 24rpx;
        text-align: center;
        color: #333;
        font-weight: 500;
      }
    }
  }
}

.community-info-section {
  background: #fff;
  border-radius: 16rpx;
  padding: 0 20rpx 20rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  
  .info-cards {
    display: flex;
    flex-direction: column;
    gap: 20rpx;
    
    .info-card {
      display: flex;
      background: #f8f9fc;
      border-radius: 12rpx;
      overflow: hidden;
      height: 160rpx;
      transition: transform 0.3s, box-shadow 0.3s;
      
      &:active {
        transform: translateY(2rpx);
        box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
      }
      
      .card-image {
        width: 160rpx;
        height: 160rpx;
        flex-shrink: 0;
        background-color: #e5e5e5;
        border-radius: 12rpx;
      }
      
      .card-content {
        flex: 1;
        padding: 20rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        
        .card-title {
          font-size: 28rpx;
          font-weight: bold;
          color: #333;
        }
        
        .card-desc {
          font-size: 24rpx;
          color: #666;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
      }
    }
  }
}
</style>